<?php
error_reporting(E_ALL ^ E_NOTICE);
include_once 'modul/mod-database.php';

function var_kirim($inp_par,$ha_nama=""){
	if(is_array($inp_par)){
		while(list($nama,$nilai)=each($inp_par)){
                        $var_post="";
			if(strpos(",$ha_nama,","$nama,")==0){
				$var_post.="&$nama=".rawurlencode($nilai)."";
			}
		}
	}

#echo "<pre>";
#print_r($inp_par);
#echo "</pre>";
	return $var_post;
}

$fungsi_pindah="<script>
function loncat(targ,selObj,restore){ //v3.0
	//alert(selObj.options[selObj.selectedIndex].value);
	window.location=selObj.options[selObj.selectedIndex].value;
	// if (restore) selObj.selectedIndex=0;
}
</script>";

function definisi_halaman($sql){
        $next="";
        $awal_prev="";
	if(isset($_REQUEST['awalan'])==""){
		$_REQUEST['awalan'] = 0;
		$next = $_REQUEST['awalan'] + 1;
	}else{
		$awal_prev = $_REQUEST['awalan'] - 1;
		$next=$_REQUEST['awalan'] + 1;
	}
	if(isset($_REQUEST['jumlah_per_hal'])=="" || isset($_REQUEST['jumlah_per_hal'])==0){
		$_REQUEST['jumlah_per_hal']=20;
	}
        
        $Id = isset($_REQUEST['Id']);
	$awalan_sql = $_REQUEST['awalan'] * $_REQUEST['jumlah_per_hal'];
	$res=dbresult($sql);
	$jumlah=mysql_num_rows($res);
	$jml=ceil($jumlah/$_REQUEST['jumlah_per_hal']);
	$jmlakhir=$jml-1;
	$str_list="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"text0\"><tr valign=\"middle\">";
	if($jumlah > $_REQUEST['jumlah_per_hal']){
		if(isset($_REQUEST['awalan'])==0){
			$str_list.="<td nowrap=\"nowrap\">".combo_jumlah_halaman($jml,var_kirim($_REQUEST,"awalan"))."&nbsp;</td><td nowrap=\"nowrap\"><a href=\"?Id=$Id&awalan=$next&kata=$_REQUEST[kata]".var_kirim($_REQUEST,"awalan")."\" title=\"Next Page\"><img src=../gambar/tombol-next.gif border=0></a><a href=\"?Id=$Id&awalan=$jmlakhir".var_kirim($_REQUEST,"awalan")."\" title=\"Last Page\"><img src=../gambar/tombol-last.gif border=0></a></td>";
		}else{
			$str_list.="<td nowrap=\"nowrap\"><a href=\"?Id=$Id&awalan=0".var_kirim($_REQUEST,"awalan")."\" title=\"First Page\"><img src=../gambar/tombol-first.gif border=0></a><a href=\"?Id=$Id&awalan=$awal_prev".var_kirim($_REQUEST,"awalan")."\" title=\"Prev Page\"><img src=../gambar/tombol-prev.gif border=0></a></td>";
			$str_list.="<td nowrap=\"nowrap\">&nbsp;".combo_jumlah_halaman($jml,var_kirim($_REQUEST,"awalan"))."&nbsp;</td>";
			if($_REQUEST['awalan']<($jml-1)){
				$str_list.="<td><a href=\"?Id=$Id&awalan=$next".var_kirim($_REQUEST,"awalan")."\" title=\"Next Page\"><img src=../gambar/tombol-next.gif border=0></a><a href=\"?Id=$Id&awalan=$jmlakhir".var_kirim($_REQUEST,"awalan")."\" title=\"Last Page\"><img src=../gambar/tombol-last.gif border=0></a></td>";
			}else{
				//	$str_list.="<font face=arial size=2 color=#527ACC>&nbsp;</font>";
			}
		}
	}
	$str_list.="</tr></table>";
	$res=dbresult($sql." limit ".$_REQUEST['jumlah_per_hal']." offset ".$awalan_sql);
        
	return array("tabel_halaman"=>$str_list,"res_sql"=>$res,"jml"=>$jml);
}

function definisi_halamanODBC($sql){
        $awal_prev="";
        $next="";
	if(isset($_REQUEST['awalan'])==""){
		$_REQUEST['awalan'] = 0;
		$next = $_REQUEST['awalan'] + 1;
	}else{
		$awal_prev = $_REQUEST['awalan'] - 1;
		$next=$_REQUEST['awalan'] + 1;
	}
	if(isset($_REQUEST['jumlah_per_hal'])=="" || isset($_REQUEST['jumlah_per_hal'])==0){
		$_REQUEST['jumlah_per_hal']=20;
	}
        $Id = isset($_REQUEST['Id']);
	$awalan_sql = $_REQUEST['awalan'] * $_REQUEST['jumlah_per_hal'];
	$res=dbresultODBC($sql);
	$jumlah=odbc_num_rows($res);
	$jml=ceil($jumlah/$_REQUEST['jumlah_per_hal']);
	$jmlakhir=$jml-1;
	$str_list="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"text0\"><tr valign=\"middle\">";
	if($jumlah > $_REQUEST['jumlah_per_hal']){
		if(isset($_REQUEST['awalan'])==0){
			$str_list.="<td nowrap=\"nowrap\">".combo_jumlah_halaman($jml,var_kirim($_REQUEST,"awalan"))."&nbsp;</td><td nowrap=\"nowrap\"><a href=\"?Id=$Id&awalan=$next&kata=$_REQUEST[kata]".var_kirim($_REQUEST,"awalan")."\" title=\"Next Page\"><img src=../gambar/tombol-next.gif border=0></a><a href=\"?Id=$Id&awalan=$jmlakhir".var_kirim($_REQUEST,"awalan")."\" title=\"Last Page\"><img src=../gambar/tombol-last.gif border=0></a></td>";
		}else{
			$str_list.="<td nowrap=\"nowrap\"><a href=\"?Id=$Id&awalan=0".var_kirim($_REQUEST,"awalan")."\" title=\"First Page\"><img src=../gambar/tombol-first.gif border=0></a><a href=\"?Id=$Id&awalan=$awal_prev".var_kirim($_REQUEST,"awalan")."\" title=\"Prev Page\"><img src=../gambar/tombol-prev.gif border=0></a></td>";
			$str_list.="<td nowrap=\"nowrap\">&nbsp;".combo_jumlah_halaman($jml,var_kirim($_REQUEST,"awalan"))."&nbsp;</td>";
			if(isset($_REQUEST['awalan'])<($jml-1)){
				$str_list.="<td><a href=\"?Id=$Id&awalan=$next".var_kirim($_REQUEST,"awalan")."\" title=\"Next Page\"><img src=../gambar/tombol-next.gif border=0></a><a href=\"?Id=$Id&awalan=$jmlakhir".var_kirim($_REQUEST,"awalan")."\" title=\"Last Page\"><img src=../gambar/tombol-last.gif border=0></a></td>";
			}else{
				//	$str_list.="<font face=arial size=2 color=#527ACC>&nbsp;</font>";
			}
		}
	}
	$str_list.="</tr></table>";
	$res=dbresultODBC($sql." limit ".$_REQUEST['jumlah_per_hal']." offset ".$awalan_sql);
        
	return array("tabel_halaman"=>$str_list,"res_sql"=>$res,"jml"=>$jml);
}

function combo_jumlah_halaman($jml,$filter){
	//global $jml;
        $Id = isset($_REQUEST['Id']);
	$txt="<select name=awalan id=awalan onChange=\"javascript:loncat('mainFrame',this,0)\" class=form2>";
	for ($i=1;$i<=$jml;$i++){
		if($_REQUEST['awalan']==($i-1)){
			$txt.="<option value=\"?Id=$Id&awalan=".($i-1)."$filter\" selected>Hal $i</option>";
		}else{
			$txt.="<option value=\"?Id=$Id&awalan=".($i-1)."$filter\">Hal $i</option>";
		}
	}
	$txt.="</select>";
	return $txt;
}

function combo_bulan($nama,$sel,$opt_all="",$dis="f", $java="",$id="",$bts_awal="",$bts_akhir="", $class=""){
	global $bulan;
	if($dis=="t"){
		$disable="disabled";
	}
	if($id!=""){
		$id=" id=\"$id\"";
	}
        $bts_awal = $bts_awal!=""?$bts_awal:1;
        $bts_akhir = $bts_akhir!=""?$bts_akhir:12;
	//$bulan=array(1=>"Jan","Feb","Mar","Apr","Mei","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	$txtout="<select name=$nama class=\"form2 $class\" $disable $java $id>";
	if($opt_all!=""){
		$txtout .= "<option value='-1' $tulis>".$opt_all."</option>";
	}
	$sel=intval($sel);
	for ($i=$bts_awal;$i<=$bts_akhir;$i++){
		if ($i==$sel){
			$tulis =" selected=\"selected\"";
		}
		$txtout .= "<option value=$i $tulis>".$bulan[$i]."</option>\n";
		$tulis = "";
	}
	$txtout .= "</select>";
	return $txtout;
}

function combo_tanggal($nama,$sel,$awal,$akhir,$opt_all="",$dis="f", $java="", $id="", $class=""){
	if($dis=="t"){
		$disable="disabled";
	}
	if($id!=""){
		$id=" id=\"$id\"";
	}else{
		$id=$nama;
	}
	$txtout="<select name=\"$nama\" class=\"form2 $class\" $disable $java $id>";
	if($opt_all!=""){
		$txtout .= "<option value='$opt_all' $tulis>".$opt_all."</option>";
	}
	for ($i=$awal;$i<=$akhir;$i++){
		$i=strlen($i)==1?"0".$i:$i;
		if ($i==$sel){
			$tulis =" selected";
		}
		$txtout .= "<option value=$i $tulis>".$i."</option>\n";
		$tulis = "";
	}
	$txtout .= "</select>";
	return $txtout;
}

function combo_array($nama,$arr_nilai,$sel,$disabled="f",$java="",$option=""){
	if($disabled=="t"){$dis="disabled";}
	$txtout.="<select name=\"$nama\" id='$nama' class=\"form2\" $dis $java>\n";
	if($option==" "){
		$txtout.="<option value=''>$option</option>";
	}
	while(list($key,$nilai)=each($arr_nilai)){
		if(trim($nilai)===trim($sel)){
			$txtout.="<option value='$nilai' selected>$nilai</option>\n";
		}else{
			$txtout.="<option value='$nilai'>$nilai</option>\n";
		}

	}
	$txtout.="</select>";
	return $txtout;
}

function comboODBC($nmtable,$nilaifld,$muncul,$nama,$option="All",$nilai="",$cek_sql="",$java="",$sid="",$disabled="f",$width="",$class=""){
		$width=$width==""?"":"style=\"width:$width\"";
                if($disabled=="t"){
                    $disabled="disabled";
                    
                }
                    $txt = "<select name=\"$nama\" size=1 class=\"form2 $class\" id=\"$sid\" $java $disabled $width>";
                if($cek_sql == ""){
                        $result=dbresultODBC("select * from $nmtable order by $nilaifld");
                        $a="select * from $nmtable order by $nilaifld";
                }else{
                        $result=dbresultODBC("$nmtable");
                }
                $jml = odbc_num_rows($result);
                if($option=="All"){
                        $txt .="<option value=-1>$option</option>\n";
                }

                if($option=="0"){
                        if($jml < 1){
                                $txt .="<option value=''>$option</option>";
                        }
                }

                if($option==" "){
                        $txt .="<option value=''>$option</option>";
                }
                $arr_muncul=split(",", $muncul);
                while ($r=odbc_fetch_array($result)){
//                        $r = odbc_fetch_array($result,$i);
                        $txtmuncul="";
                        for ($x=0;$x<count($arr_muncul);$x++){
                            $txtmuncul.=$r[$arr_muncul["$x"]].' ';
                        }
                        $title=$width==""?"":"title='$r[$muncul]'";
                        if(trim($r["$nilaifld"])==trim($nilai)){
                                $txt .= "<option value='$r[$nilaifld]' $title selected>".trim($txtmuncul)."</option>\n";
                        }else{
                                $txt .= "<option value='$r[$nilaifld]' $title>".trim($txtmuncul)."</option>\n";
                        }
//                        echo $r[$nilaifld]." : ".$r[$muncul]."<br>";
                }
                $txt .= "</select>";
                return $txt;
    }
    
function comboODBCPDO($nmtable,$nilaifld,$muncul,$nama,$option="All",$nilai="",$cek_sql="",$java="",$sid="",$disabled="f",$width="",$class=""){
		$width=$width==""?"":"style=\"width:$width\"";
                if($disabled=="t"){
                    $disabled="disabled";
                    
                }
                    $txt = "<select name=\"$nama\" size=1 class=\"form $class\" id=\"$sid\" $java $disabled $width>";
                if($cek_sql == ""){
                        $result=dbresultODBCPDO("select * from $nmtable order by $nilaifld");
                        $a="select * from $nmtable order by $nilaifld";
                }else{
                        $result=dbresultODBCPDO("$nmtable");
                }
                $jml = $result->rowCount();
                if($option=="All"){
                        $txt .="<option value=-1>$option</option>\n";
                }
                if($option=="0"){
                        if($jml < 1){
                                $txt .="<option value=''>$option</option>";
                        }
                }

                if($option==" "){
                        $txt .="<option value=''>$option</option>";
                }
                $arr_muncul=  explode(",", $muncul);
                while ($r=$result->fetch(PDO::FETCH_ASSOC)){
//                        $r = odbc_fetch_array($result,$i);
                        $txtmuncul="";
                        for ($x=0;$x<count($arr_muncul);$x++){
                            $txtmuncul.=$r[$arr_muncul["$x"]].' ';
                        }
                        $title=$width==""?"":"title='$r[$muncul]'";
                        if(trim($r["$nilaifld"])==trim($nilai)){
                                $txt .= "<option value='$r[$nilaifld]' $title selected>".trim($txtmuncul)."</option>\n";
                        }else{
                                $txt .= "<option value='$r[$nilaifld]' $title>".trim($txtmuncul)."</option>\n";
                        }
//                        echo $r[$nilaifld]." : ".$r[$muncul]."<br>";
                }
                $txt .= "</select>";
                return $txt;
}

function sqlkearray($sql){
	$res_item=dbresult($sql);
	while($r=pg_fetch_row($res_item)){
		$jumlah=count($r);
		if($jumlah==1){
			$arr_y["$r[0]"]=$r[0];
		}elseif($jumlah==2){
			$arr_y["$r[0]"]=$r[1];
		}elseif($jumlah==3){
			$arr_y["$r[0]"]["$r[1]"]=$r[2];
		}elseif($jumlah==4){
			$arr_y["$r[0]"]["$r[1]"]["$r[2]"]=$r[3];
		}
	}
	return $arr_y;
}

function format_dk($angka,$digit=0,$xx="",$yy=""){
	if($angka <=0){
		$str_temp= number_format((0-$angka),$digit)." K";
	}else{
		$str_temp= number_format($angka,$digit). " D";
	}
	return $str_temp;
}

function ctr_array($arr){
		echo "<pre>";
		print_r($arr);
		echo "</pre>";
}
        
 function getTotalDay($tahun,$bulan){
    if ($bulan==2){
        if ($tahun%4==0){
            $hari = 29;
        }else if($tahun%4!=0){
            $hari = 28;
        }
    }else if(($bulan==4 || $bulan==6 || $bulan==9 || $bulan==11)){
        $hari = 30;
    }else{
        $hari = 31;
    }
    return $hari;
}

function getTotalDayWD($tahun,$bulan,$hari){
    if ($bulan==2){
        if ($tahun%4==0 && $hari>29){
            $hari = 29;
        }else if($tahun%4!=0 && $hari>28){
            $hari = 28;
        }
    }else if(($bulan==4 || $bulan==6 || $bulan==9 || $bulan==11) && $hari>30){
        $hari = 30;
    }
    return $hari;
}


?>